//
//  AYMCityTableViewController.m
//  QuickCure
//
//  Created by 张聪 on 16/5/5.
//  Copyright © 2016年 ayumi. All rights reserved.
//

#import "AYMCityTableViewController.h"
#import "AYMAreaModel.h"

@implementation AYMCityTableViewController

//懒加载

- (NSArray *)citys {
    
    if (_citys == nil) {
        
        //数据路径
        NSString *path = [[NSBundle mainBundle] pathForResource:@"province.plist" ofType:nil];
        
        NSArray *citiesArray = [NSArray arrayWithContentsOfFile:path];
        
        //字典转模型
        NSMutableArray *tempArray = [[NSMutableArray alloc] init];
        
        for (NSDictionary *dict in citiesArray) {
            
            AYMAreaModel *areaModel = [AYMAreaModel provinceWithDict:dict];
            
            [tempArray addObject:areaModel];
        }
        
        _citys = tempArray.copy;
    }
    return _citys;
}

static NSString *const ID = @"reuseIdentifier";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //注册cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    

    return self.citys.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
    
    cell.textLabel.text = self.citys[indexPath.row];
    
    return cell;
}

#pragma mark - 点击了cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {    
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"areaNotification" object:self userInfo:@{@"areaKey" : self.citys[indexPath.row]}];
    Log(@"%@",self.citys[indexPath.row]);
    
    //刷新天气通知
    [[NSNotificationCenter defaultCenter]postNotificationName:@"weatherNotification" object:self];
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    
}


@end
